package com.kgc.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.mapper.NewsdetailMapper;
import com.kgc.pojo.Newscomment;
import com.kgc.pojo.Newsdetail;
import com.kgc.pojo.NewsdetailExample;
import com.kgc.service.NewscommentService;
import com.kgc.service.NewsdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/detail")
public class NewsdetailController {
    @Autowired
    private NewsdetailService newsdetailService;

    @Autowired
    private NewscommentService newscommentService;

    @RequestMapping("/list")
    public String detlist(Model model){
        List<Newsdetail> newsdetails=newsdetailService.selectNewsdetailAll();
        model.addAttribute("newsdetails",newsdetails);
        return "jsp/invlist";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        newsdetailService.deleteNewsdetail(id);
        return "redirect:/detail/mlist";
    }

    @RequestMapping("/mlist")
    public String mohulist(@RequestParam(required = false) String title,
                           @RequestParam(required = false,defaultValue = "1")Integer pageNo,
                           @RequestParam(required = false,defaultValue = "3")Integer pageSize,
                           Model model){
        //调
        PageInfo<Newsdetail> pageInfo=newsdetailService.selectNewsdetailByCondition(title,pageNo,3);
        List<Newscomment> newscomments=newscommentService.selectNewscommentAll();

        //存
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("title",title);
        model.addAttribute("newscomments",newscomments);


        return "jsp/invlist";
    }
}
